/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28318
 * Date: 2025-10-31
 * Time: 16:27
 */

import java.util.Scanner;

/**
 * 处理用户输入的工具类
 */
public class InputHandler {
    private Scanner scanner;

    public InputHandler() {
        scanner = new Scanner(System.in);
    }

    // 读取整数
    public int readInt(String prompt, int min, int max) {
        int value;
        while (true) {
            try {
                System.out.print(prompt);
                value = Integer.parseInt(scanner.nextLine().trim());
                if (value >= min && value <= max) {
                    return value;
                } else {
                    System.out.println("请输入" + min + "到" + max + "之间的整数！");
                }
            } catch (NumberFormatException e) {
                System.out.println("输入无效，请输入一个整数！");
            }
        }
    }

    // 读取资源数组
    public int[] readResourceArray(String prompt, int length) {
        int[] array = new int[length];
        while (true) {
            try {
                System.out.print(prompt);
                String[] parts = scanner.nextLine().trim().split("\\s+");
                if (parts.length != length) {
                    System.out.println("请输入" + length + "个整数，用空格分隔！");
                    continue;
                }

                boolean valid = true;
                for (int i = 0; i < length; i++) {
                    array[i] = Integer.parseInt(parts[i]);
                    if (array[i] < 0) {
                        valid = false;
                        break;
                    }
                }

                if (valid) {
                    return array;
                } else {
                    System.out.println("资源数量不能为负数！");
                }
            } catch (NumberFormatException e) {
                System.out.println("输入无效，请输入整数！");
            }
        }
    }

    // 读取矩阵
    public int[][] readMatrix(String name, int rows, int cols) {
        int[][] matrix = new int[rows][cols];
        System.out.println("请输入" + name + "矩阵（每行" + cols + "个整数，用空格分隔）：");

        for (int i = 0; i < rows; i++) {
            while (true) {
                try {
                    System.out.print("进程P" + i + "：\n");
                    String[] parts = scanner.nextLine().trim().split("\\s+");
                    if (parts.length != cols) {
                        System.out.println("请输入" + cols + "个整数！");
                        continue;
                    }

                    boolean valid = true;
                    for (int j = 0; j < cols; j++) {
                        matrix[i][j] = Integer.parseInt(parts[j]);
                        if (matrix[i][j] < 0) {
                            valid = false;
                            break;
                        }
                    }

                    if (valid) {
                        break;
                    } else {
                        System.out.println("资源数量不能为负数！");
                    }
                } catch (NumberFormatException e) {
                    System.out.println("输入无效，请输入整数！");
                }
            }
        }
        return matrix;
    }

    // 关闭扫描器
    public void close() {
        scanner.close();
    }
}
